package cn.tedu.micro.demo.order.service;

import cn.tedu.micro.demo.device.common.pojo.po.OrderInfoPO;
import cn.tedu.micro.demo.device.common.pojo.protocol.JsonResult;
import cn.tedu.micro.demo.device.common.pojo.vo.GunInfoVO;
import cn.tedu.micro.demo.order.feign.DeviceFeign;
import cn.tedu.micro.demo.order.pojo.entity.Order;
import cn.tedu.micro.demo.order.pojo.dto.OrderCommitParam;
import cn.tedu.micro.demo.order.repository.OrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService{
    @Autowired
    OrderRepository orderRepository;
    @Autowired
    DeviceFeign deviceFeign;

    @Override
    public String createOrder(OrderCommitParam orderCommitParam) {
        JsonResult<GunInfoVO> result = deviceFeign.getGunInfo(Long.valueOf(orderCommitParam.getGunId()));
        if (result != null && result.getData().getStatus() == 1) {
            log.debug("获取请求参数{}",Long.valueOf(orderCommitParam.getGunId()));
            log.debug("设备可用,参数{}",result.getData());
            OrderInfoPO order = new OrderInfoPO();
            BeanUtils.copyProperties(orderCommitParam,order);
            order.setBillId(UUID.randomUUID().toString());
            order.setGunId(Long.valueOf(orderCommitParam.getGunId()));
            order.setUserId(Long.valueOf(orderCommitParam.getUserId()));
            order.setCreateTime(new Date());
            order.setUpdateTime(new Date());
            orderRepository.insert(order);
            return order.getBillId();
        }else {
            log.debug("设备不可用,参数{}",result.getData());
            return null;
        }
    }
}
